﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Urbania.World
{
    public class WorldHelper
    {

        public static int GetIndexOfHex(int xi, int yi, int w, int h)
        {
            if (xi >= 0 && xi < w && yi >= 0 && yi < h)
            {
                return (xi + w * yi);
            }
            else
            {
                return -1;
            }
        }

        public static int GetIndexOfHex(Vector2 indVec, int w, int h)
        {
            return GetIndexOfHex((int)indVec.X, (int)indVec.Y, w, h);
        }

        public static int FloorToInt(float num)
        {
            if (num >= 0)
            {
                return (int)(num);
            }
            else
            {
                return (int)(num - 1.0f);
            }
        }

        public static Vector2 GetAdjacentTileIndex(int xind, int yind, int adjacIndex)
        {
            bool xindIsEven = (xind % 2 == 0);
            int xi = 0;
            int yi = 0;
            if (adjacIndex == 0)
            {
                xi = xind;
                yi = yind - 1;
            }
            else if (adjacIndex == 1)
            {
                xi = xind - 1;
                if (xindIsEven)
                {
                    yi = yind - 1;
                }
                else
                {
                    yi = yind;
                }
            }
            else if (adjacIndex == 2)
            {
                xi = xind - 1;
                if (xindIsEven)
                {
                    yi = yind;
                }
                else
                {
                    yi = yind + 1;
                }
            }
            else if (adjacIndex == 3)
            {
                xi = xind;
                yi = yind + 1;
            }
            else if (adjacIndex == 4)
            {
                xi = xind + 1;
                if (xindIsEven)
                {
                    yi = yind;
                }
                else
                {
                    yi = yind + 1;
                }
            }
            else if (adjacIndex == 5)
            {
                xi = xind + 1;
                if (xindIsEven)
                {
                    yi = yind - 1;
                }
                else
                {
                    yi = yind;
                }
            }
            return new Vector2(xi, yi);
        }


    }
}
